/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.xml.objecttype;

import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;


/**
 *
 * @author Xe Bay
 */
public class phatsinhTypeBaseOnSchema {

    public static void main(String[] args) {
        String ouputLocation = "src/java/";
        String ouputPackage = "nhadat.xml.objecttype.donvitiente";
        String schemaPath = "web/xml/dsDonViTienTeSchema.xsd";

//        generateJavaOjb(ouputLocation, ouputPackage, schemaPath);
//
//        ouputLocation = "src/java/";
//        ouputPackage = "nhadat.xml.objecttype.loaigiayto";
//        schemaPath = "web/xml/dsLoaiGiayToSchema.xsd";
//
//        generateJavaOjb(ouputLocation, ouputPackage, schemaPath);
//
//        ouputLocation = "src/java/";
//        ouputPackage = "nhadat.xml.objecttype.loainha";
//        schemaPath = "web/xml/dsLoaiNhaSchema.xsd";
//
//        generateJavaOjb(ouputLocation, ouputPackage, schemaPath);
//
//        ouputLocation = "src/java/";
//        ouputPackage = "nhadat.xml.objecttype.loaitin";
//        schemaPath = "web/xml/dsLoaiTinSchema.xsd";
//
//        generateJavaOjb(ouputLocation, ouputPackage, schemaPath);

        ouputLocation = "src/java/";
        ouputPackage = "nhadat.tindang";
        schemaPath = "web/xml/Tindang.xsd";

        generateJavaOjb(ouputLocation, ouputPackage, schemaPath);

    }

    private static void generateJavaOjb(String ouputLocation, String outputPackage, String schemaPath) {
        try {
            SchemaCompiler sc = XJC.createSchemaCompiler();
            sc.setErrorListener(new ErrorListener() {

                public void error(SAXParseException saxpe) {
                    System.out.println("Error:" + saxpe.getMessage());
                }

                public void fatalError(SAXParseException saxpe) {
                    System.out.println("Error:" + saxpe.getMessage());
                }

                public void warning(SAXParseException saxpe) {
                    System.out.println("Error:" + saxpe.getMessage());
                }

                public void info(SAXParseException saxpe) {
                    System.out.println("Error:" + saxpe.getMessage());
                }
            });
            sc.forcePackageName(outputPackage);
            File schema = new File(schemaPath);
            InputSource is = new InputSource(schema.toURI().toString());
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind();
            JCodeModel code = model.generateCode(null, null);
            code.build(new File(ouputLocation));
        } catch (IOException ex) {
            Logger.getLogger(phatsinhTypeBaseOnSchema.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
